﻿using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DongFangGuoKai.WMSPlatform.Service.BasicApp.Cell
{
    public class CellService:ICellService
    {
        private readonly SimpleRepositroy<TCells> cellrepost;

        public CellService(SimpleRepositroy<TCells> cellrepost)
        {
            this.cellrepost = cellrepost;
        }
        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="logicalPartCode"></param>
        /// <param name="fullStatus"></param>
        /// <param name="status"></param>
        /// <param name="cname"></param>
        /// <param name="whid"></param>
        /// <param name="areid"></param>
        /// <param name="sheid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TCells> GetCellList(string logicalPartCode, int fullStatus, int status, string cname, int whid, int areid, int sheid, int pageIndex, int pageSize, out int totalCount)
        {
            var clist = cellrepost.GetList();
            //逻辑分区
            if (!string.IsNullOrEmpty(logicalPartCode))
            {
                clist = clist.Where(x => x.LogicalPartCode == logicalPartCode).ToList();
            }
            //仓位状态
            if (fullStatus < 3)
            {
                clist = clist.Where(x => x.FullStatus == fullStatus).ToList();
            }
            //状态
            if (status < 8)
            {
                clist = clist.Where(x => x.Status == status).ToList();
            }
            //仓位名称
            if (!string.IsNullOrEmpty(cname))
            {
                clist = clist.Where(x => x.Name.Contains(cname)).ToList();
            }
            //库房
            if (whid != 0)
            {
                clist = clist.Where(x => x.WhId == whid).ToList();
            }
            //库区
            if (areid != 0)
            {
                clist = clist.Where(x => x.AreaId == areid).ToList();
            }
            //货架
            if (sheid != 0)
            {
                clist = clist.Where(x => x.ShelfId == sheid).ToList();
            }
            totalCount = clist.Count();
            return clist.OrderBy(x => x.Code).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 获取Excel仓位数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<TCells>? GetExcelCellData(int[] ids)
        {
            List<TCells> clist = new();
            foreach (int id in ids) 
            {
                var cell = cellrepost.GetFirst(c=>c.Id == id);
                if (cell == null)
                {
                    return null;
                }
                clist.Add(cell);
            }
            return clist;
        }

        /// <summary>
        /// 获取全部仓位
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TCells> GetAllCellList()
        {
            try
            {
                return  cellrepost.GetList();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool DelCellList(List<TCells> cells)
        {
            if (cellrepost.Delete(cells))
            {
                return true;
            }
            return false;
        }

        public bool AddCellList(List<TCells> tcell)
        {
            if (cellrepost.InsertRange(tcell))
            {
                return true;
            }
            return false;
        }
    }
}
